﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Command;
using HIWSystem.Common;
using HIWSystem.Helpers;
using HIWSystem.Models;

namespace HIWSystem.ViewModel
{
    public class PartNumberQueryViewModel : ViewModelBase
    {
        #region 属性
        private string pn;
        /// <summary>
        /// 机种
        /// </summary>
        public string PN
        {
            get { return pn; }
            set { pn = value; this.RaisePropertyChanged(() => PN); }
        }

        private string  pnType;
        /// <summary>
        /// 产品类型
        /// </summary>
        public string  PnType
        {
            get { return pnType; }
            set { pnType = value; this.RaisePropertyChanged(() => PnType); }
        }
        private ObservableCollection<PartNumberEntity> pnTypes;

        public ObservableCollection<PartNumberEntity> PnTypes
        {
            get { return pnTypes; }
            set { pnTypes = value; this.RaisePropertyChanged(() => PnTypes); }
        }
        private PartNumberEntity selectedPnType;

        public PartNumberEntity SelectedPnType
        {
            get { return selectedPnType; }
            set { selectedPnType = value; this.RaisePropertyChanged(() => SelectedPnType); }
        }
        private ObservableCollection<PartNumberEntity> pns;
        /// <summary>
        /// 机种数据信息
        /// </summary>
        public ObservableCollection<PartNumberEntity> PNs
        {
            get { return pns; }
            set { pns = value; this.RaisePropertyChanged(() => PNs); }
        }
        private Visibility isVisibleLoading=Visibility.Collapsed;
        /// <summary>
        /// 是否显示加载图标
        /// </summary>
        public Visibility IsVisibleLoading
        {
            get { return isVisibleLoading; }
            set { isVisibleLoading = value; this.RaisePropertyChanged(() => IsVisibleLoading); }
        }
        private bool isEnabledQueryButton=true;
        /// <summary>
        /// 是否禁用查询按钮
        /// </summary>
        public bool IsEnabledQueryButton
        {
            get { return isEnabledQueryButton; }
            set { isEnabledQueryButton = value; this.RaisePropertyChanged(() => IsEnabledQueryButton); }
        }

        #endregion

        #region 命令
        private RelayCommand loadedPageCmd;

        public RelayCommand LoadedPageCmd
        {
            get {
                if (loadedPageCmd==null)
                {
                    loadedPageCmd = new RelayCommand(LoadedPage);
                }
                return loadedPageCmd; }
            set { loadedPageCmd = value; }
        }
        private RelayCommand queryPnCmd;

        public RelayCommand QueryPnCmd
        {
            get {
                if (queryPnCmd==null)
                {
                    queryPnCmd = new RelayCommand(QueryPnAsync);
                }
                return queryPnCmd; }
            set { queryPnCmd = value; }
        }
        #endregion

        #region 方法
        /// <summary>
        /// 页面加载
        /// </summary>
        private void LoadedPage()
        {
            PnTypes = new ObservableCollection<PartNumberEntity>();
            PnTypes.Add(new PartNumberEntity { PnType = "滤波器" });
            PnTypes.Add(new PartNumberEntity { PnType = "天线" });
        }
        /// <summary>
        /// 查询机种信息
        /// </summary>
        private async void QueryPnAsync()
        {
            string retMsg = string.Empty;
            string[] arrMsg = new string[2];
            ObservableCollection<PartNumberEntity> tmpPNs = new ObservableCollection<PartNumberEntity>();
            try
            {
                await Task.Run(() =>
                {
                    Application.Current.Dispatcher.BeginInvoke((Action)(() =>
                    {
                        //禁用查询按钮
                        this.IsEnabledQueryButton = false;
                        //显示加载图标
                        this.IsVisibleLoading = Visibility.Visible;
                        PNs = new ObservableCollection<PartNumberEntity>();
                    }));
                    PartNumberEntity entity = new PartNumberEntity()
                    {
                        PN = this.PN ?? "",
                        PnType = this.PnType ?? ""
                    };
                    DataTable dtPn = new DataTable();
                    retMsg = BusinessProcess.QueryPartNumber(entity, out dtPn);
                    arrMsg = retMsg.Split('|');
                    if (arrMsg[0] == "Y")
                    {
                        for (int i = 0; i < dtPn.Rows.Count; i++)
                        {
                            tmpPNs.Add(new PartNumberEntity
                            {
                                PN = dtPn.Rows[i]["PartNumber"].ToString() ?? "",
                                SnFormat = dtPn.Rows[i]["SnFormat"].ToString() ?? "",
                                UnitCode = dtPn.Rows[i]["UnitCode"].ToString() ?? "",
                                KitCount = Convert.ToInt32(dtPn.Rows[i]["KitCount"].ToString() ?? "0"),
                                PrintCopiesNum = Convert.ToInt32(dtPn.Rows[i]["PrintCopiesNum"].ToString() ?? "0"),
                                PnType = dtPn.Rows[i]["PnType"].ToString() ?? "",
                                PerBoxNum = Convert.ToInt32(dtPn.Rows[i]["PerBoxNum"].ToString() ?? "0"),
                                LPCId = new LabelConfigEntity
                                {
                                    RowNum = Convert.ToInt32(dtPn.Rows[i]["RowNum"].ToString() ?? "0"),
                                    TemplateName = dtPn.Rows[i]["TemplateName"].ToString() ?? "",
                                    FormVariables = dtPn.Rows[i]["FormVariables"].ToString() ?? ""
                                },
                                CreateAt = dtPn.Rows[i]["CreateAt"].ToString() ?? ""
                            });
                        }
                        Application.Current.Dispatcher.BeginInvoke((Action)(() =>
                        {
                            this.PNs = tmpPNs;
                        }));
                    }
                    else
                    {
                        MessageBox.Show(arrMsg[1], "软件温馨提示", MessageBoxButton.OK, MessageBoxImage.Error);
                    }
                    Application.Current.Dispatcher.BeginInvoke((Action)(() =>
                    {
                        this.IsVisibleLoading = Visibility.Collapsed;//隐藏加载动画
                        this.IsEnabledQueryButton = true;
                    }));

                });
            }
            catch (Exception ex)
            {
                LogHelper.WriteLog(string.Format("执行方法【{0}】出现错误【{1}】", nameof(QueryPnAsync), ex.Message), ex);
                throw new Exception(ex.Message);
            }
        }
        #endregion
    }
}
